# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import scrapy

from scrapy import Request
import os

from scrapy.pipelines.media import MediaPipeline


class PachonPipeline(MediaPipeline):
    def get_media_requests(self, item, info):
        # print('get_media_requests')
        request = scrapy.Request(
            url=item['file_url'],
            method="GET",
            headers={
                "Accept": "*/*",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:63.0) Gecko/20100101 Firefox/63.0",
                "Connection": "keep-alive"
            },
            meta={"item": item}
        )
        return request

    def media_downloaded(self, response, request, info):
        # (vpath, vname) = os.path.split(request.url)
        item = response.meta['item']

        filepath = os.path.join(item['file_floder'], item['filename'])
        # print('media_downloaded:' + filepath)
        print(request.url)
        if not os.path.exists(filepath):
            print('media_downloaded开始下载:' + filepath)
            with open(filepath, "wb") as f:
                f.write(response.body)
            print('下载完成：'+filepath)
        else:
            print('已存在：'+filepath)
        # print("download complete %s for %s" % (request.url, filepath))

    def media_failed(self, failure, request, info):
        print('下载失败')
        print(failure)
